package com.wanpinghui.supplier.product.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.product.bean.CartItemFormatBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/21
 ***/

public class CartFormatAdapter extends BaseAdapter {
    private Context context;
    private List<CartItemFormatBean> datas;
    private OnCountChangeListener listener;

    public void setDatas(List<CartItemFormatBean> list) {
        datas = list;
        notifyDataSetChanged();
    }

    public void setOnCountChangeListener(OnCountChangeListener listener) {
        this.listener = listener;
    }

    public CartFormatAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_select_format, null);
        TextView textViewName = (TextView) view.findViewById(R.id.tv_name);
        TextView textViewPrice = (TextView) view.findViewById(R.id.tv_price);
        final TextView textViewMinues = (TextView) view.findViewById(R.id.tv_minues);
        TextView textViewPlus = (TextView) view.findViewById(R.id.tv_plus);
        final YunShlEditText editText = (YunShlEditText) view.findViewById(R.id.edt_count);
        textViewName.setPadding(DensityUtil.dip2px(30), 0, 0, 0);
        final CartItemFormatBean bean = datas.get(position);
        textViewName.setText(bean.getSpec_value());
        if (bean.isIs_consult()) {
            textViewPrice.setText("议价");
        } else {
            textViewPrice.setText("￥" + NumberUtil.double2String(bean.getPrice()));
        }
        //textViewPrice.setText(NumberUtil.double2StringAdaptive(bean.getPrice()));
        editText.setText(Integer.toString(bean.getQuantity()));
        if (bean.getQuantity() > 0) {
            textViewMinues.setVisibility(View.VISIBLE);
            editText.setVisibility(View.VISIBLE);
            editText.setText(Integer.toString(bean.getQuantity()));
        } else {
            textViewMinues.setVisibility(View.GONE);
            editText.setVisibility(View.GONE);
            editText.setText("");
        }
        textViewPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setQuantity(bean.getQuantity() + 1);
                editText.setText(Integer.toString(bean.getQuantity()));
            }
        });
        textViewMinues.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getQuantity() > 0) {
                    bean.setQuantity(bean.getQuantity() - 1);
                    editText.setText(Integer.toString(bean.getQuantity()));
                }
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    int count = Integer.parseInt(s.toString());
                    bean.setQuantity(count);
                }
                if (bean.getQuantity() > 0) {
                    textViewMinues.setVisibility(View.VISIBLE);
                    editText.setVisibility(View.VISIBLE);
                } else {
                    textViewMinues.setVisibility(View.GONE);
                    editText.setVisibility(View.GONE);
                }
                if (listener != null) {
                    listener.onChange(position, bean);
                }
            }
        });
        return view;
    }

    public interface OnCountChangeListener {
        void onChange(int pos, CartItemFormatBean formatBean);
    }
}
